using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using NTTHAO.Web.BL.Contact;
using NTTHAO.CommonWeb;

public partial class ContactSubmit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string toName = System.Configuration.ConfigurationManager.AppSettings["CONTACT_NAME"];
            string toEmail = System.Configuration.ConfigurationManager.AppSettings["CONTACT_EMAIL"];
            //NTTHAO.Web.BL.CommonPage.CommonSite site = NTTHAO.CommonWeb.WebGlobal.GetSite(Session);
            
            string fName = Request.Form["fname"];
            string email = Request.Form["email"];
            string phone = Request.Form["phone"];
            string message = Request.Form["message"];
            
            try
            {
                ContactDetail detail = new ContactDetail();
                detail.GroupID = 1;
                detail.Name = "Contact from " + fName;
                detail.Name = detail.Name.Length > 255 ? detail.Name.Substring(0, 255) : detail.Name;
                detail.ContactIPAddress = WebGlobal.GetClientIpAddress(Request);
                detail.Insert();

                ContactDetailContent content = new ContactDetailContent();
                content.DetailID = detail.ID;

                content.Key = "NAME";
                content.Content = fName;
                content.Insert();
                content.Key = "EMAIL";
                content.Content = email;
                content.Insert();
                content.Key = "PHONE";
                content.Content = phone;
                content.Insert();
                content.Key = "MESSAGE";
                content.Content = message;
                content.Insert();
            }
            catch { }

            message = message.Replace("\n", "<br />");

            MailMessage mm = new MailMessage();
            mm.From = new MailAddress(email, fName, System.Text.Encoding.UTF8);
            mm.To.Add(new MailAddress(toEmail, toName, System.Text.Encoding.UTF8));
            mm.Bcc.Add(new MailAddress("admin@hoacucdai.net", "Thao Nguyen", System.Text.Encoding.UTF8));
            mm.Subject = "Contact email from " + fName;
            string strBoby = "Contact email from website <a href=\"http://" + Request.Url.Host + "\">" + Request.Url.Host + "</a>."
                + "<br /><br />From: " + fName
                + "<br />Email: <a href=\"mailto:" + email + "\">" + email + "</a>"
                + "<br />Phone: " + phone
                + "<br />Message:"
                + "<br /><br />" + message;
            mm.Body = strBoby;
            mm.IsBodyHtml = true;
            mm.ReplyTo = new MailAddress(email, fName, System.Text.Encoding.UTF8);

            NTTHAO.CommonWeb.WebGlobal.SendMail(mm);

            string finishedUrl = "";
            if (Request.Form["ReturnUrl"] != null)
            {
                finishedUrl = Request.Form["ReturnUrl"].Trim();
            }
            else
            {
                finishedUrl = System.Configuration.ConfigurationManager.AppSettings["CONTACT_FINISHED_URL_" + NTTHAO.CommonWeb.LanguageUtility.GetLanguageID(Request, Session).ToString()];
            }
            
            if (finishedUrl == null)
            {
                finishedUrl = "/";
            }
            Response.Redirect(finishedUrl);
        }
        catch (Exception ex) 
        {
            Response.Write(ex.Message + "<br /><br />");
            Response.Write(ex.StackTrace);
            return;
        }
    }
}
